export default {
  formatDate(date, fmt) {
    if (/(y+)/.test(fmt)) {
      fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length))
    }
    const o = {
      'M+': date.getMonth() + 1,
      'd+': date.getDate(),
      'h+': date.getHours(),
      'm+': date.getMinutes(),
      's+': date.getSeconds()
    }
    for (const k in o) {
      if (new RegExp(`(${k})`).test(fmt)) {
        const str = o[k] + ''
        fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? str : this.padLeftZero(str))
      }
    }
    return fmt
  },
  padLeftZero(str) {
    return ('00' + str).substr(str.length)
  },
  formatTime(timeLong, unit) {
    if (!timeLong) {
      return 0
    }
    timeLong = parseFloat(timeLong)
    if (unit === 's') {
      timeLong = timeLong * 1000
    }
    var timeData = ''
    if (timeLong >= 3600000) {
      timeData += (parseInt(timeLong / 3600000) + 'h')
      timeLong = timeLong % 3600000
    }
    if (timeLong >= 60000) {
      timeData += (parseInt(timeLong / 60000) + 'm')
      timeLong = timeLong % 60000
    }
    if (timeLong >= 1000 || unit === 's') {
      timeData += (parseInt(timeLong / 1000) + 's')
      timeLong = timeLong % 1000
    }
    if (unit !== 's') {
      timeData += (parseInt(timeLong) + 'ms')
    } else {
      timeLong === 0 ? timeData : timeData += (parseInt(timeLong) + 'ms')
    }
    return timeData
  }

}
